package com.sigmatrix.h5.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@Configuration
@ComponentScan
public class Swagger {
	private final String description = "返回结果数据结构：{status:\"200\",msg:\"成功\",pageNo:1,pageSize:20,total:20,data:{model....}}";
	
	private static List<Parameter> params = new ArrayList<Parameter>();
	static {
		params.add(new ParameterBuilder().description("eseId")
		.name("eseId")
		.parameterType("query")
		.modelRef(new ModelRef("Integer"))
		.required(false).build());

		params.add(new ParameterBuilder().description("token")
		.name("token")
		.parameterType("query")
		.modelRef(new ModelRef("String"))
		.required(false).build());
	}
	
	@Bean
	public Docket dealerDocket() {
		Docket docket = new Docket(DocumentationType.SWAGGER_2);
		ApiInfo apiInfo = new ApiInfoBuilder().contact(new Contact("麻恒", "", "maheng@sao.so"))
		.description(description)
		.title("经销商反向注册H5")
		.build();
		return docket.apiInfo(apiInfo)
		.groupName("经销商反向注册H5")
		.genericModelSubstitutes(Result.class)
		.globalOperationParameters(params)
		.select()
		.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
		.apis(RequestHandlerSelectors.basePackage("com.sigmatrix.h5.web.dealer.controller"))
		.paths(PathSelectors.any())
		.build();
	}
	@Bean
	public Docket insDocket() {
		Docket docket = new Docket(DocumentationType.SWAGGER_2);
		ApiInfo apiInfo = new ApiInfoBuilder().contact(new Contact("麻恒", "", "maheng@sao.so"))
				.description(description)
				.title("稽查H5")
				.build();
		return docket.apiInfo(apiInfo)
				.groupName("稽查H5")
				.genericModelSubstitutes(Result.class)
				.globalOperationParameters(params)
				.select()
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
				.apis(RequestHandlerSelectors.basePackage("com.sigmatrix.h5.web.ins.controller"))
				.paths(PathSelectors.any())
				.build();
	}		
	@Bean
	public Docket rebateDocket() {
		Docket docket = new Docket(DocumentationType.SWAGGER_2);
		ApiInfo apiInfo = new ApiInfoBuilder().contact(new Contact("麻恒", "", "maheng@sao.so"))
				.description(description)
				.title("返利H5")
				.build();
		return docket.apiInfo(apiInfo)
				.groupName("返利H5")
				.genericModelSubstitutes(Result.class)
				.globalOperationParameters(params)
				.select()
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
				.apis(RequestHandlerSelectors.basePackage("com.sigmatrix.h5.web.rebate"))
				.paths(PathSelectors.any())
				.build();
	}
	
	@Bean
	public Docket distributionDocket() {
		Docket docket = new Docket(DocumentationType.SWAGGER_2);
		ApiInfo apiInfo = new ApiInfoBuilder().contact(new Contact("高明凯", "", "gaomingkai@sao.so"))
				.description("")
				.title("分销App")
				.build();
		return docket.apiInfo(apiInfo)
				.groupName("分销App")
				.genericModelSubstitutes(Result.class)
				.globalOperationParameters(params)
				.select()
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
				.apis(RequestHandlerSelectors.basePackage("com.sigmatrix.h5.web.controller"))
				.paths(PathSelectors.any())
				.build();
	}
	
	@Bean
	public Docket openDocket() {
		Docket docket = new Docket(DocumentationType.SWAGGER_2);
		ApiInfo apiInfo = new ApiInfoBuilder().contact(new Contact("ww", "", "ww@sao.so"))
				.description("")
				.title("front接口（h5和小程序调用）")
				.build();
		List<Parameter> list = new ArrayList<Parameter>();
		 Parameter auth = new ParameterBuilder().description("token")
					.name("token")
					.parameterType("header")
					.modelRef(new ModelRef("String"))
					.required(true).build();
		 list.add(auth);
		return docket.apiInfo(apiInfo)
				.groupName("front接口")
				.genericModelSubstitutes(Result.class)
				.globalOperationParameters(list)
				.select()
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
				.apis(RequestHandlerSelectors.basePackage("com.sigmatrix.h5.web.controller.front"))
				.paths(PathSelectors.any())
				.build();
	}
	
	@Bean
	public Docket utilDocket() {
		Docket docket = new Docket(DocumentationType.SWAGGER_2);
		ApiInfo apiInfo = new ApiInfoBuilder().contact(new Contact("ww", "", "ww@sao.so"))
				.description("")
				.title("util接口（测试使用）")
				.build();
		List<Parameter> list = new ArrayList<Parameter>();
		return docket.apiInfo(apiInfo)
				.groupName("util接口")
				.genericModelSubstitutes(Result.class)
				.globalOperationParameters(list)
				.select()
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
				.apis(RequestHandlerSelectors.basePackage("com.sigmatrix.h5.web.controller.utils"))
				.paths(PathSelectors.any())
				.build();
	}
}
